package com.wt.test.ai.springai.manager;

import cn.hutool.core.text.CharSequenceUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Component;

/**
 * 聊天管理器
 *
 * @author qiyu
 * @since 2025/1/25
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class ChatManager {
    
    private static final String CHAT_MEMORY_CONVERSATION_ID_KEY = "chat_memory_conversation_id";
    
    private static final String CHAT_MEMORY_RESPONSE_SIZE_KEY = "chat_memory_response_size";
    
    private final ChatClient chatClient;
    
    /**
     * 调用聊天机器人接口，生成回复
     *
     * @param chatId    聊天ID
     * @param userInput 用户输入
     * @param voice     语言风格
     * @return 机器人回复
     */
    public String conversation(String chatId, String userInput, String voice) {
        if(CharSequenceUtil.isBlank(voice)){
            voice = "周星驰";
        }
        final String voiceFinal = voice;
        return chatClient.prompt()
                .user(userInput)
                .system(sp -> sp.param("voice", voiceFinal))
                .advisors(a -> a
                        .param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RESPONSE_SIZE_KEY, 1000))
                .call()
                .content();
    }
}
